package com.springSpringMVC.action;

import com.springSpringMVC.dao.UserDao;
import com.springSpringMVC.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.test.web.servlet.setup.SharedHttpSessionConfigurer;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author hasee
 */
@Controller
public class UserAction {
    private UserDao userDao;

    @RequestMapping("/showReg")
    public String showReg(){
        return "reg";
    }

    @RequestMapping("/reg")
    public String reg(User user){
        userDao.save(user);
        return "success";
    }
    @RequestMapping("/login")
    public String Login(String userName,String userPassWord){
        //todo
    User user=    userDao.login(userName,userPassWord);
    if (!user.equals(null)){
        //登录成功
        return "success";
    }else {
        //登录失败
        return "login";
    }


    }
    @RequestMapping("/showLogin")
    public String showLogin(){
        return "login";
    }
    @RequestMapping("/showAllUser")
    public String allUser(Model model){
      List<User> userList= userDao.listAllUser();
      model.addAttribute("userList",userList);
      return "allUser";
    }
    @RequestMapping("/delete")
    public String delete(@RequestParam (required = true ,name="userId")  int userId){
        userDao.deleteUser(userId);
        //无参数跳转
        return "redirect:/showAllUser";
    }
    @RequestMapping("/ShowUpdata")
    public String ShowUpdata(@RequestParam int  userId,Model model){
        User user= userDao.selectUserById(userId);
        model.addAttribute("user",user);
        return "upUser";
    }
    @RequestMapping("/update")
    public String update(User user ,HttpSession sesion){
        sesion
        if (user.getUserId()>0) {
            userDao.update(user);
        }
        return "redirect:/showAllUser";
    }

    @Autowired
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }
}
